package com.wisedu.w5autocode.util;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Group;

/**
 * 
 * 
 */
public class Tools {

	/**
	 * 
	 * @param path
	 */
	public static void createDirectory(String path) {
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
		file.mkdirs();
	}

	/**
	 * 创建文
	 * 
	 * @param path
	 * @return
	 */
	public static void createFile(String path) {
		File file = new File(path);
		if (file.exists()) {
			file.delete();
		}
		try {
			file.createNewFile();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	/**
	 * 
	 * @param filePath
	 * @param content
	 */
	public static void writeStringToFile(String filePath, String content) {
		File file = new File(filePath);
		try {
			FileUtils.writeStringToFile(file, content, "utf-8");
		} catch (IOException e) {
			e.printStackTrace();
		}

	}

	public static void insertStringToFile(String filePath, String content, String end) {
		File file = new File(filePath);
		try {
			String str = FileUtils.readFileToString(file, "GBK");
			str = StringUtils.stripEnd(str, null);
			FileUtils.writeStringToFile(file, StringUtils.removeEnd(str, end) + IOUtils.LINE_SEPARATOR + content + IOUtils.LINE_SEPARATOR + end, "GBK");
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static Composite createComposite(Composite parent) {
		Composite composite = new Composite(parent, SWT.NONE);
		GridLayout layout = new GridLayout();
		layout.marginWidth = 0;
		layout.marginHeight = 0;
		composite.setLayout(layout);
		composite.setLayoutData(new GridData(GridData.VERTICAL_ALIGN_FILL | GridData.HORIZONTAL_ALIGN_FILL));
		return composite;
	}

	public static Group createGroup(Composite composite, String title, int nc) {
		Group groupComposite = new Group(composite, SWT.LEFT);
		GridLayout layout = new GridLayout();
		layout.numColumns = nc;
		groupComposite.setLayout(layout);
		GridData data = new GridData(GridData.FILL_HORIZONTAL | GridData.VERTICAL_ALIGN_FILL);

		groupComposite.setLayoutData(data);
		groupComposite.setText(title);
		return groupComposite;
	}

}
